const mock = require("mockjs");
const brandList = [
	{
		title: "奥迪",
		type: "A",
		id: 1,
	},
	{
		title: "奥拓",
		type: "A",
		id: 2,
	},
	{
		title: "阿尔法",
		type: "A",
		id: 3,
	},
	{
		title: "奔驰",
		type: "B",
		id: 4,
	},
	{
		title: "长城",
		type: "C",
		id: 5,
	},
	{
		title: "长安",
		type: "C",
		id: 6,
	},
];
const { carList } = mock.mock({
	"carList|100": [
		{
			title: "@ctitle",
			"price|1-1000": 1,
			id: "@id",
			"brand|1": [1, 2, 3, 4, 5, 6],
			"type|1": ["轿车", "轿跑", "SUV"],
			"image|6":["@image(100x100,@color)"],
			"carType|3": [
				{
					title: "@ctitle",
					desc: "@ctitle",
					"price|1-1000": 1,
				}
			]
		},
	],
});
const {imageList} = mock.mock({
	"imageList|100":[{
		"type|1": [1,2,3], // 1 外观 2 内饰  3 空间
		"image":"@image(100x100,@color)",
	}]
})
module.exports = {
	devServer: {
		before(app) {
			// 车品牌
			app.get("/brand", (req, res) => {
				res.send({
					success: true,
					data: brandList,
				});
			});
			// 车系 query body params
			app.get("/car/list/:id", (req, res) => {
				const { id } = req.params;
				let newList = carList.filter(item => item.brand == id);
				res.send({
					success: true,
					data: newList,
				});
			});
			// 获取详情接口
			app.get('/car/detail/:id', (req,res) => {
				const { id } = req.params;
				const data = carList.find(item => item.id==id)
				if(data) {
					res.send({
						success:true,
						data: data
					})
				} else {
					res.send({
						success:false,
						data: ""
					})
				}
				
			})
			// 获取图片接口 只获取每个区域的前6张
			app.get('/car/image',(req,res)=>{
				let imgObj = {}
				imageList.forEach(item => {
					if(imgObj[item.type]) {
						imgObj[item.type].push(item.image)
					} else {
						imgObj[item.type] = [item.image]
					}
				})
				const imgList = [
					{
						type:1,
						total: imgObj[1].length,
						imageList:  imgObj[1].slice(0,6)
					},
					{
						type:2,
						total: imgObj[2].length,
						imageList: imgObj[2].slice(0,6)
					},
					{
						type:3,
						total: imgObj[3].length,
						imageList: imgObj[3].slice(0,6)
					}
				];
				res.send({
					success:true,
					data:imgList
				})
			})
			// 根据区域获取全部图片
			// axios.get('/car/imageList/1')
			app.get('/car/imageList/:type',(req,res) => {
				const {type} = req.params;
				const imgList = imageList.filter(item => item.type==type)
				res.send({
					success:true,
					data:imgList
				})
			})
		},
	},
};
